<?php

namespace App\Http\Controllers\manage;

use App\app;
use App\app_comment;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class commentController extends Controller
{
    function index()
    {
        $apps = app::all();
        $title = "从网络导入";
        return view('manage\net', compact('title', 'apps'));
    }

    function saveComment(Request $request)
    {
        $startDate = $request->get("startDate");
        $endData = date("Y-m-d", time() - 24 * 3600);
        $gameId = $request->get("gameId");
        $nextPage = $request->get("nextPage");
        $maxPage = $request->get("maxPage");
        $categoryId = $request->get("categoryId");
        $AID = app::where("sourceID", $gameId)->first()->AID;

        $json = getComment($startDate, $endData, $gameId, $nextPage, $maxPage, $categoryId);
        $data = json_decode($json);
        $return = array();
        $return['page'] = $nextPage;
        $return['add'] = 0;

        foreach ($data->ret->searchDatas as $arr) {
            if(app_comment::where([
                'from_who' => $arr->author,
                'text' => $arr->content,
                'AID' => $AID,
                'time' => $arr->createtime,
            ])->count()){
                continue;
            }

            $comment = new app_comment;
            $comment->fill(['from_who' => $arr->author,
                'from_where' => $arr->categoryComment,
                'rating' => $arr->rank,
                'text' => $arr->content,
                'time' => $arr->createtime,
                'AID' => $AID,
                "from_id" => $categoryId,
                'tasty' => 0]);
            $comment->save();
            $return['add']++;
        }
        return json_encode($return);
    }

    function getData(Request $request)
    {
        $startDate = $request->get("startDate");
        $endData = date("Y-m-d", time() - 24 * 3600);
        $gameId = $request->get("gameId");
        $nextPage = $request->get("nextPage");
        $maxPage = $request->get("maxPage");
        $categoryId = $request->get("categoryId");

        $data = getComment($startDate, $endData, $gameId, $nextPage, $maxPage, $categoryId);
        return $data;
    }
}
